import unittest
from wisdom.models.news import News 
import json

class TestNews(unittest.TestCase):
    def setUp(self):
        # 设置测试用例
        self.news = News()
        self.news.title = "Old Title"
        self.news.content = "Old Content"
        self.news.source = "Old Source"
        self.news.published_date = "2023-01-01"
        self.news.url = "http://oldurl.com"
        self.news.category = "Old Category"
        self.news.author = "Old Author"
        self.news.listed_company = "Old Listed Company"
        self.news.fin_tags = json.dumps(["tag1", "tag2"])
        self.news.stock_list = json.dumps(["stock1", "stock2"])

        self.new_item = News()
        self.new_item.url = self.news.url
        self.new_item.title = self.news.title
        self.new_item.content = self.news.content
        self.new_item.source = self.news.source
        self.new_item.published_date = self.news.published_date
        self.new_item.category = self.news.category
        self.new_item.author = self.news.author
        self.new_item.listed_company = self.news.listed_company
        self.new_item.fin_tags = self.news.fin_tags
        self.new_item.stock_list = self.news.stock_list
        self.new_item.thumbnail = self.news.thumbnail
        

    def test_no_update_needed_for_basic_fields(self):
        # 测试相同基本字段是否不需要更新
        self.news.title = "New Title"
        self.assertTrue(self.news.check_if_update_needed(self.new_item))

    def test_update_needed_for_json_fields(self):
        # 测试 JSON 字段变化是否需要更新
        self.new_item.fin_tags = json.dumps(["tag1", "tag3"])  # 改变一个标签
        self.assertTrue(self.news.check_if_update_needed(self.new_item))

    def test_no_update_needed_for_json_fields(self):
        # 测试相同的 JSON 字段是否不需要更新
        self.assertFalse(self.news.check_if_update_needed(self.new_item))